<template>
  <div class='container'>
    <el-card class="box-card">
      <el-form :inline="true" :model="requestParams" class="demo-form-inline">
        <el-form-item label="关键字">
          <el-input v-model="requestParams.keyword"></el-input>
        </el-form-item>
        <el-form-item label="状态" class="ipt" >
          <el-select v-model="requestParams.state" placeholder="请选择">
            <el-option :value="1" label="启用"></el-option>
            <el-option :value="0" label="禁用"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button @click.native="clear">清除</el-button>
          <el-button @click.native="filter" type="primary" >搜索</el-button>
        </el-form-item>
        <el-button type="success" class="right" @click="openDialog()" size="small" >
          <i class="el-icon-edit"></i>
          新增技巧
        </el-button>
      </el-form>
      <el-alert
        :title="`数据一共${total}条`"
        style="margin-bottom:15px"
        type="info"
        :closable="false"
        class="alert"
        show-icon
      ></el-alert>
      <!-- 表格 -->
      <el-table :data="articles">
        <el-table-column label="序号" type="index" width="80px"></el-table-column>
        <el-table-column label="文章标题" width="400px">
          <template v-slot="{ row }">
            {{row.title}}
            <a @click.prevent="openVideo(row.videoURL)" v-if="row.videoURL" href="#" class="el-icon-film"></a>
          </template>
        </el-table-column>
        <el-table-column label="阅读数" prop="visits"></el-table-column>
        <el-table-column label="录入人" prop="username"></el-table-column>
        <el-table-column label="录入时间">
          <template v-slot="{ row }">{{row.createTime|parseTimeByString}}</template>
        </el-table-column>
        <el-table-column label="状态" width="100px">
          <template v-slot="{ row }">{{row.state===1?'已启用':'已禁用'}}</template>
        </el-table-column>
        <el-table-column label="操作" width="180px">
          <template v-slot="{ row }">
            <el-button type="text" @click="previewArticle(row)">预览</el-button>
            <el-button type="text" @click="toggleState(row)">{{row.state===1?'禁用':'启用'}}</el-button>
            <el-button type="text" @click="openDialog(row)" :disabled="row.state===1">修改</el-button>
            <el-button type="text" @click="delArticle(row)" :disabled="row.state===1||row.totals > 0">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        style="margin-top:20px;text-align:right"
        background
        layout="prev, pager, next, sizes, jumper"
        :total="total"
        @current-change="change"
        @size-change="size"
        :page-size="requestParams.pagesize"
        :current-page="requestParams.page"
        :page-sizes="[5,10,20,50]"
        hide-on-single-page
      ></el-pagination>
    </el-card>
    <!-- 添加|修改 -->
    <ArticlesAdd ref="articlesAdd" :data="currArticle" @updateList.native="updateList" />
    <!-- 预览 -->
    <ArticlesPreview ref="articlesPreview" :data="currArticle"/>
    <!-- 视频 -->
    <div class="video-preview" v-if="videoURL">
      <div class="close" @click="closeVideo"><span class="el-icon-close"></span></div>
      <div class="box">
        <video ref="video" :src="videoURL"></video>
      </div>
    </div>
  </div>
</template>

<script>
import { list, changeState, remove } from '@/api/hmmm/articles'
import ArticlesAdd from '../components/articles-add'
import ArticlesPreview from '../components/articles-preview'
export default {
  name: 'Articles',
  components: { ArticlesAdd, ArticlesPreview },
  data () {
    return {
      requestParams: {
        keyword: null,
        state: null,
        page: 1,
        pagesize: 10
      },
      total: 0,
      articles: [],
      videoURL: null,
      currArticle: {}
    }
  },
  created () {
    this.getList()
  },
  methods: {
    // 查询列表
    async getList () {
      const { data } = await list(this.requestParams)
      this.articles = data.items
      this.total = data.counts
      console.log(data, this.articles)
    },
    clear () {
      this.requestParams = {
        keyword: null,
        state: null,
        page: 1,
        pagesize: 10
      }
    },
    filter () {
      this.requestParams.page = 1
      this.getList()
    },
    change (page) {
      // console.log(page)
      this.requestParams.page = page
      this.getList()
    },
    size (pagesize) {
      // console.log(pagesize)
      this.requestParams.pagesize = pagesize
      this.requestParams.page = 1
      this.getList()
    },
    // 视频
    openVideo (url) {
      // console.log(url)
      this.videoURL = url
      this.$nextTick(() => {
        this.$refs.video.play()
      })
    },
    closeVideo () {
      this.$refs.video.pause()
      this.videoURL = null
    },
    previewArticle (row) {
      this.currArticle = row
      this.$nextTick(() => {
        this.$refs.articlesPreview.open()
      })
    },
    async toggleState (article) {
      await changeState({
        id: article.id,
        state: article.state === 1 ? 0 : 1
      })
      this.$message.success('操作成功')
      article.state = article.state === 1 ? 0 : 1
    },
    // 添加文章, 编辑文章功能
    openDialog (article = {}) {
      this.currArticle = article
      this.$nextTick(() => {
        this.$refs.articlesAdd.open()
      })
    },
    // 新增|修改 后更新列表
    updateList () {
      if (!this.currArticle.id) {
        this.requestParams.page = 1
      }
      this.getList()
    },
    // 删除
    async delArticle (article) {
      await this.$confirm('此操作将永久删除该文章, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
      await remove(article)
      this.$message.success('删除成功')
      this.getList()
    }
  }
}
</script>

<style scoped lang='scss'>
.box-card {
  margin: 20px 20px;
  .ipt {
    margin-left: 30px;
  }
  .right {
    float: right;
  }
  .el-icon-film {
    color: blue;
  }
}
.video-preview{
    position: fixed;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    background: rgba(0,0,0,.3);
    overflow: hidden;
    z-index: 9999;
    .close{
      width: 50px;
      height: 50px;
      position: absolute;
      top: 30px;
      left: 50%;
      transform: translate(-50%,0);
      background: rgba(0,0,0,0.4);
      box-shadow: 0 0 5px rgba(0,0,0,0.4);
      border-radius: 50%;
      text-align: center;
      line-height: 50px;
      color: #fff;
      font-size: 20px;
      z-index: 999;
    }
    .box{
      width: 800px;
      height: 600px;
      position: absolute;
      left: 50%;
      top: 50%;
      transform: translate(-50%,-50%);
      video{
        width: 100%;
        height: 100%;
      }
    }
  }
</style>
